package com.bawei.module_goods.ui

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.TextView
import com.bawei.module_goods.R
import android.widget.CompoundButton

class CartBottomBarView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : LinearLayout(context, attrs) {

    val checkboxSelectAll: CheckBox
    val textTotalPrice: TextView
    val btnCheckout: Button
    val textDetail: TextView
    val layoutNormal: LinearLayout
    val layoutEdit: LinearLayout
    val checkboxSelectAllEdit: CheckBox
    val btnDelete: Button
    val btnCollect: Button

    init {
        orientation = HORIZONTAL
        LayoutInflater.from(context).inflate(R.layout.layout_cart_bottom_bar, this, true)
        checkboxSelectAll = findViewById(R.id.checkboxSelectAll)
        textTotalPrice = findViewById(R.id.textTotalPrice)
        btnCheckout = findViewById(R.id.btnCheckout)
        textDetail = findViewById(R.id.textDetail)
        layoutNormal = findViewById(R.id.layoutNormal)
        layoutEdit = findViewById(R.id.layoutEdit)
        checkboxSelectAllEdit = findViewById(R.id.checkboxSelectAllEdit)
        btnDelete = findViewById(R.id.btnDelete)
        btnCollect = findViewById(R.id.btnCollect)
    }

    fun setTotalPrice(price: Double) {
        textTotalPrice.text = "合计: ￥${price.toInt()}"
    }

    fun setCheckedAll(checked: Boolean, listener: CompoundButton.OnCheckedChangeListener?) {
        checkboxSelectAll.setOnCheckedChangeListener(null)
        checkboxSelectAll.isChecked = checked
        if (listener != null) {
            checkboxSelectAll.setOnCheckedChangeListener(listener)
        }
    }

    fun setCheckedAllEdit(checked: Boolean, listener: CompoundButton.OnCheckedChangeListener?) {
        checkboxSelectAllEdit.setOnCheckedChangeListener(null)
        checkboxSelectAllEdit.isChecked = checked
        if (listener != null) {
            checkboxSelectAllEdit.setOnCheckedChangeListener(listener)
        }
    }

    fun setCheckoutCount(count: Int) {
        btnCheckout.text = "去结算($count)"
    }

    fun setEditMode(edit: Boolean) {
        layoutNormal.visibility = if (edit) View.GONE else View.VISIBLE
        layoutEdit.visibility = if (edit) View.VISIBLE else View.GONE
    }

    fun setDeleteCount(count: Int) {
        btnDelete.text = "删除($count)"
    }

    fun setCollectCount(count: Int) {
        btnCollect.text = "移入收藏($count)"
    }
} 